<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-02-24
 * Time: 12:15
 */

namespace app\service;

use app\model\AcceptOrder;
use app\model\Customer;
use app\model\RepairOrder;
use app\model\RepairPerson;
use app\model\ServiceConfig;

class RepairService extends BaseService{


    function __construct()
    {
        parent::__construct();
    }

    function count($where)
    {
        $where['status'] = ['in', [20, 30, 36]]; // 20, 30, 36, 40
        $in = RepairOrder::where($where)->count();
        $waitEvaluate = RepairOrder::alias('r')
            ->join('accept_order a', 'a.o_id = r.id')
            ->where([
                'r.status'=> 40,
                'a.status'=> 40,
                'a.uid'=> 0,
                'r.customer_id'=> $where['customer_id']
            ])->count();

        return [
            'in'=> $in,
            'waitEvaluate'=> $waitEvaluate
        ];
    }

    function eavList($where)
    {
        $where['a.uid'] = ['>', 0];

        $data = AcceptOrder::alias('a')
            ->join('customer c', 'c.id = a.uid')
            ->where($where)
            ->field('a.customer_id, a.eva, a.star, c.avatar_url, c.nickname, a.eva_date')
            ->paginate(20);

        return $data;
    }

    function masterInfo($master_id)
    {
        $data = AcceptOrder::where([
            'customer_id'=> $master_id,
            'uid'=> ['>', 0]
        ])->field('sum(star) as star, count(id) as num')->find();

        $user = RepairPerson::where(['customer_id'=> $master_id])->field('fullname, avatar')->find();
        if($data['num'] > 0)
        {
            $user['score'] = intval($data['star'] / $data['num']);
        }else{
            $user['score'] = 0;
        }
        return $user;
    }

    function masterList($where)
    {
        $where2 = "status = 3 or status = 5 and thaw_time < '".date('Y-m-d H:i:s')."'";
        $data = RepairPerson::where($where)->where($where2)->field('customer_id, avatar, fullname, service_type')->paginate(6)->each(function (&$item){
            $item['count'] = AcceptOrder::where(['customer_id'=> $item['customer_id']])->count();
            $data = AcceptOrder::where(['customer_id'=> $item['customer_id'], 'uid'=> ['>', 0]])->field('sum(star) as star, count(id) as num')->find();
            if($data['num'] > 0)
            {
                $item['score'] = intval($data['star'] / $data['num']);
            }else{
                $item['score'] = 0;
            }
            $service = ServiceConfig::where(['id'=> ['in', $item['service_type']]])->column('name');
            $item['service'] = $service;
            switch (true)
            {
                case $data['star'] < 15:
                    $item['level_name'] = '铜牌师傅';
                    $item['level'] = 1;
                    break;
                case $data['star'] < 500:
                    $item['level_name'] = '银牌师傅';
                    $item['level'] = 2;
                    break;
                case $data['star'] < 5000:
                    $item['level_name'] = '金牌师傅';
                    $item['level'] = 3;
                    break;
                default:
                    $item['level_name'] = '钻石师傅';
                    $item['level'] = 4;
                    break;

            }
            return $item;
        });
        return $data;
    }


}